package com.myprocessing.box.button;

import com.myprocessing.action.Action;
import com.myprocessing.box.Box;
import com.myprocessing.box.MouseListener;

public class Button extends Box implements MouseListener{

	protected Action mouseOver;
	protected Action mouseEnter;
	protected Action mouseExit;
	protected Action mousePress;
	protected Action notMouseOver;
	protected Action keyPress;
	protected Action notKeyPress;
	protected boolean hasMouseOver = false;

	public void init() {
		super.init();

		initAction(mouseOver);
		initAction(mouseEnter);
		initAction(mouseExit);
		initAction(mousePress);
		initAction(notMouseOver);
		initAction(keyPress);
		initAction(notKeyPress);
	}
	
	protected void initAction(Action action){
		if(action != null){
			action.init();
		}
	}
	
	public boolean isValidMouseOver() {
		return ((getAbsolutePosition().getX() <= pApplet.mouseX)
				&& (getAbsolutePosition().getX() + size.getX() >= pApplet.mouseX)
				&& (getAbsolutePosition().getY() <= pApplet.mouseY) && 
				(getAbsolutePosition().getY() + size.getY() >= pApplet.mouseY) && isActive());

	}
	
	public void onMousePress() {
		if(isValidMouseOver()){
			executeAction(mousePress);

			for (Box box : subBoxSet) {
				box.onMousePress();
			}
		}
	}

	public void onMouseOver() {
		if (isValidMouseOver()) {
			executeAction(mouseOver);

			for (Box box : subBoxSet) {
				box.onMouseOver();
			}
		}
	}

	public void onMouseEnter() {
		if (isValidMouseOver()
				&& !hasMouseOver) {
			executeAction(mouseEnter);

			for (Box box : subBoxSet) {
				box.onMouseEnter();
			}
			
			hasMouseOver = true;
		}
	}

	public void onMouseExit() {
		if (!isValidMouseOver()
				&& hasMouseOver){
			executeAction(mouseExit);

			for (Box box : subBoxSet) {
				box.onMouseExit();
			}
			
			hasMouseOver = false;
		}
	}

	public void onNotMouseOver() {
		if (!isValidMouseOver()){
			executeAction(notMouseOver);

			for (Box box : subBoxSet) {
				box.onNotMouseOver();
			}
		}
	}

	public void executeAction(Action action){
		if(action != null){
			action.execute();
		}
	}

	public Action getMouseOver() {
		return mouseOver;
	}

	public void setMouseOver(Action mouseOver) {
		this.mouseOver = mouseOver;
	}

	public Action getMouseEnter() {
		return mouseEnter;
	}

	public void setMouseEnter(Action mouseEnter) {
		this.mouseEnter = mouseEnter;
	}

	public Action getMouseExit() {
		return mouseExit;
	}

	public void setMouseExit(Action mouseExit) {
		this.mouseExit = mouseExit;
	}

	public Action getMousePress() {
		return mousePress;
	}

	public void setMousePress(Action mousePress) {
		this.mousePress = mousePress;
	}

	public Action getNotMouseOver() {
		return notMouseOver;
	}

	public void setNotMouseOver(Action notMouseOver) {
		this.notMouseOver = notMouseOver;
	}

	public Action getKeyPress() {
		return keyPress;
	}

	public void setKeyPress(Action keyPress) {
		this.keyPress = keyPress;
	}

	public Action getNotKeyPress() {
		return notKeyPress;
	}

	public void setNotKeyPress(Action notKeyPress) {
		this.notKeyPress = notKeyPress;
	}
}
